<?php
// super parser include
include ($install_dir."/include/s_parse.php");

// dB connection
include ($install_dir."/include/db.php");

//$res_id;

/*
$query = "SELECT * FROM recipe GROUP BY rec_id ASC";
   	 $result = mysql_query($query) or die(mysql_error());
	 while ($row = mysql_fetch_array($result)) {
	 print_r($row);
}
*/

if  ($mode == NULL && $rec_id == NULL) {
	 
	 /* output a list of categorys */
	 echo "<table>\n<tr>\n<th>Select by category</th>\n</tr>\n";
	 $query = "SELECT * FROM recipe GROUP BY rec_cat ASC";
   	 $result = mysql_query($query) or die(mysql_error());
	 while ($row = mysql_fetch_array($result)) {
	 echo "<tr>\n<td><a href=\"$install_url/recipe.php?mode=listcat&rec_cat=$row[rec_cat]\">$row[rec_cat]</a></td>\n</tr>\n";
	 }
	 echo "</table>\n";
	 
	 /* output a list of main ingredients *//*
	 echo "<table>\n<tr>\n<th>Select by main ingredient</th>\n</tr>\n";
	 $query = "SELECT * FROM recipe GROUP BY rec_sub_cat ASC";
   	 $result = mysql_query($query) or die(mysql_error());
	 while ($row = mysql_fetch_array($result)) {
	 echo "<tr>\n<td><a href=\"$install_url/recipe.php?mode=listmain&rec_sub_cat=$row[rec_sub_cat]\">$row[rec_sub_cat]</a></td>\n</tr>\n";
	 }*//*removed section to clean up page   </table>\n */
	 echo "<br />\n<b><a href=\"$install_url/recipe.php?mode=listall\">List All</a></b><br />\n<b><a href=\"$install_url/\">Back to MPC home.</a></b><br />\n";
}

elseif ($mode == "listcat" && $rec_id == NULL) {

	 /* output a list of recipes by catigory */
	 echo "<b>Select a catigory</b><br />\n";
	 $query = "SELECT * FROM recipe WHERE rec_cat='$rec_cat' ORDER BY rec_name ASC";
   	 $result = mysql_query($query) or die(mysql_error());
	 while ($row = mysql_fetch_array($result)) {
	 echo "<a href=\"$install_url/recipe.php?rec_id=$row[rec_id]\">$row[rec_name]</a> - $row[rec_sub_cat]<br />\n";
	 }
	 echo "<br />\n<b><a href=\"$install_url/recipe.php?mode=listall\">List All</a></b><br />\n<b><a href=\"$install_url/recipe.php\">Back to list of catigories.</a></b><br />\n";
}

elseif ($mode == "listmain" && $rec_id == NULL) {

	 /* output a list of resipes by main ingredient */
	 echo "<b>Select a recipe</b><br />\n";
	 $query = "SELECT * FROM recipe WHERE rec_sub_cat='$rec_sub_cat' ORDER BY rec_name ASC";
   	 $result = mysql_query($query) or die(mysql_error());
	 while ($row = mysql_fetch_array($result)) {
	 echo "<a href=\"$install_url/recipe.php?rec_id=$row[rec_id]\">$row[rec_name]</a> - $row[rec_cat]<br />\n";
	 }
	 echo "<br />\n<b><a href=\"$install_url/recipe.php?mode=listall\">List All</a></b><br />\n<b><a href=\"$install_url/recipe.php\">Back to list of catigories.</a></b><br />\n";
}

elseif ($mode == "listall" && $rec_id == NULL) {
	 echo "<b>Select a recipe</b><br />\n";
	 $query = "SELECT * FROM recipe ORDER BY rec_name ASC";
   	 $result = mysql_query($query) or die(mysql_error());
	 while ($row = mysql_fetch_array($result)) {
	 echo "<a href=\"$install_url/recipe.php?rec_id=$row[rec_id]\">$row[rec_name]</a><br />\n";
	 }
	 echo "<br />\n<b><a href=\"$install_url/recipe.php\">Back to list of catigories.</a></b><br />\n";
}
	 
elseif ($mode == NULL && $rec_id != NULL) {

	 /* output a resipe */
	 $query = "SELECT * FROM recipe WHERE rec_id=$rec_id";
   	 $result = mysql_query($query) or die(mysql_error());
   	 while ($row = mysql_fetch_array($result)) {
		 ?>
		 <div class="rec_name"><?php echo "<b>$row[rec_name]</b> - $row[rec_cat] - $row[rec_sub_cat] - Quick Meal: "; if ($row[rec_qm] == 0) {echo "No";} else {echo "Yes";} ?></div>
		 <div class="rec_source"><?php echo "By: <i>$row[rec_source]</i> Serves: <i>$row[rec_serves]</i> Prep-Time: <i>$row[rec_prep]</i> Cook-Time: <i>$row[rec_cook]</i>"; ?> </div>
		 <div class="title">Ingredients</div>
		 <p><?php echo parse($row[rec_ingred]); ?></p>
		 <div class="title">Directions</div>
		 <p><?php echo parse($row[rec_direct]); ?></p>
		 <div class="title">Notes</div>
		 <p><?php echo parse($row[rec_notes]); ?></p>
		 <?php
	}
	echo "<b><a href=\"./recipe.php\">Back to list of recipes.</a></b><br />\n";
}
?>